from time import sleep
from selenium.webdriver.support.wait import WebDriverWait
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions
class BasePage():
    def __init__(self,driver):
        self.driver = driver
    # 打开项目地址  ,并且设置隐式等待
    def open(self,url,timeout):
        self.driver.get(url)
        self.driver.maximize_window()
        self.driver.implicitly_wait(timeout)
    # 获取 元素定位方法
    def converter(self,target): # (By.ID,KW)  target = "id=kw" 'xpath=XXXXX'
        by ,value =target.split('=',1)
        return by.strip() ,value.strip()  # strip() 去除by 和 value 左右两边的空格

    def locate_element(self,target):
        by,value = self.converter(target)

        ele = self.driver.find_element(by,value)
        return ele
    def input(self,target,value):
        self.locate_element(target).send_keys(value)

    def button(self,target):
        self.locate_element(target).click()
    
    def switch_to_frame(self, frame_locator):
        """
        切换到指定的iframe框架
        :param frame_locator: 框架的定位方式（可以是id/name/index/WebElement）
        """
        self.driver.switch_to.frame(frame_locator)
    
    def switch_to_default_content(self):
        """切换回主文档（最外层）"""
        self.driver.switch_to.default_content()

    def select_by_text(self,target,text):
        ele = self.locate_element(target)
        select_object = Select(ele)
        select_object.select_by_visible_text(text)

    def select_by_index(self,target,index):
        ele = self.locate_element(target)
        select_object = Select(ele)
        select_object.select_by_index(index)

    def select_by_value(self,target,value):
        ele = self.locate_element(target)
        select_object = Select(ele)
        select_object.select_by_value(value)

    def wait(self,secend):
        sleep(secend)

    def quit(self):
        self.driver.quit()

    def get_text(self,target):
        return self.locate_element(target).text
# 封装登录页面
class loginPage(BasePage):
    def __init__(self,driver):
        super().__init__(driver)
        # 页面元素的定位方式
        self.account_loc="id=account"
        self.password_loc="id=password"
        self.submit_loc = "id=submit"

    def type_account(self,name):
        self.input(self.account_loc,name)

    def type_password(self,password):
        self.input(self.password_loc,password)
    def type_submit(self):
        self.button(self.submit_loc)
class MainPage(BasePage):
    def __init__(self, driver):
        super().__init__(driver)
        # 页面元素的定位方式
        # self.account_loc="id=account"
        # self.password_loc="id=password"
        self.submit_loc ='xpath=//*[@id="s-menu-1"]/button'
        self.submit_loc1='xpath=//*[@id="s-menu-superadmin"]/button'
        self.submit_loc2 = 'xpath=//*[@id="bottomRightBar"]/ul/li[1]/a'
    def kehu_click(self):
        self.button(self.submit_loc)
    def houtai_click(self):
        self.button(self.submit_loc1)
    def qiantui_click(self):
        self.button(self.submit_loc2)

    
    
if __name__ == '__main__':
    driver = webdriver.Chrome()
    bs = BasePage(driver=driver)
    lg = loginPage(driver=driver)
    lg.open("http://localhost/ranzhi/www",10)
    lg.type_account("admin")
    lg.wait(2)
    lg.type_password("123456")
    lg.wait(2)
    lg.type_submit()
    lg.wait(2)
    main = MainPage(driver=driver)
    main.kehu_click()
    main.wait(3)
    main.houtai_click()
    main.wait(3)
    hou=BackPage(driver=driver)
    hou.add_click()
    hou.wait(3)
    hou.backs()
    hou.wait(3)
    # bs.switch_to_default_content()

    main.qiantui_click()
    main.wait(3)
    lg.quit()
# 封装  首页 MainPage()    客户管理 ,  后台管理,签退
#
#       后台管理页面  back_page()  成员添加: